#!/bin/bash

# $Header: /opt/unixmon/tools/DNS/RCS/named_process_zones,v 1.115 2016/02/02 09:27:24 yang Exp $

export PATH=/bin:/usr/bin

# ROOT="/MINE"
ROOT=""

TMPDIR="${ROOT}/var/named/tmp"
CONFDIR="${ROOT}/etc/named"

COVERTER="${ROOT}/usr/local/sbin/named_process_3pp_zones.pl"
ZONECAT="${ROOT}/usr/local/sbin/zonecat -T"
CHKCONF=/opt/bind/sbin/named-checkconf

MPDNSUSER="root@int-dns11.phx.ebay.com"
QADNSUSER="itadmin@10.109.104.20"
# QA SERVERS:
# 10.109.104.20
# 10.109.104.21
# 10.109.107.73

com=$(basename $0)

print_usage()
{
echo "
Usage: 
	$com -mp	
		- Copy zone configuration file from MarketPlace OPS, and generate a configuration file for IT master DNS server.

	$com -qa
		- Copy zone configuration file from MarketPlace QA, and generate a configuration file for IT master DNS server.

" >&2
}

# ------------------- MAIN start ---------------------
if [ $# -ne 1 ]; then
	print_usage
	exit 1
fi


case $1 in 
     '-mp')
	OTHERZONES=${TMPDIR}/allzones-mp
	cd $CONFDIR; $ZONECAT root.zones base.zones its.zones qa.zones misc.zones | awk '{print $1}' > $OTHERZONES
	scp -q ${MPDNSUSER}:/etc/named-slave/named.zones $TMPDIR/mp.zones.raw
	$COVERTER -mp $TMPDIR/mp.zones.raw > $TMPDIR/mp.zones-new
	if [ $? -ne 0 ]; then 
		echo "Failed to convert $TMPDIR/mp.zones.raw." >&2
		exit 4
	fi
	$CHKCONF $TMPDIR/mp.zones-new
	if [ $? -ne 0 ]; then
		echo "$TMPDIR/mp.zones-new failed to pass syntax checking. " >&2
		exit 3
	else
		cp -p $TMPDIR/mp.zones-new $CONFDIR
		exit 0
	fi	
	;;
     '-qa')
	OTHERZONES=${TMPDIR}/allzones-qa
	cd $CONFDIR; $ZONECAT root.zones base.zones its.zones mp.zones misc.zones | awk '{print $1}' > $OTHERZONES
	scp -q ${QADNSUSER}:/etc/qa.zones $TMPDIR/qa.zones.raw
	$COVERTER -qa $TMPDIR/qa.zones.raw > $TMPDIR/qa.zones-new
	if [ $? -ne 0 ]; then 
		echo "Failed to convert $TMPDIR/qa.zones.raw." >&2
		exit 4
	fi
	$CHKCONF $TMPDIR/qa.zones-new
	if [ $? -ne 0 ]; then
		echo "$TMPDIR/qa.zones-new failed to pass syntax checking. " >&2
		exit 3
	else
		cp -p $TMPDIR/qa.zones-new $CONFDIR 
		exit 0
	fi	
	;;
     *)
	print_usage
	exit 2
esac

# ------------------- MAIN end ---------------------
